package tclp.gui;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import net.sourceforge.javaocr.ocrPlugins.mseOCR.TrainingImage;
import org.jnativehook.keyboard.NativeKeyEvent;
import tclp.Settings;
import tclp.SettingsLoader;
import tclp.gui.panels.MainPanel;
import tclp.listener.ActivationKeyListener;

/**
 *
 * @author Absatz Leoš
 */
public class GUIController {

  private MainFrame mainFrame;

  private Settings settings;

  private ActivationKeyListener keyListener;
  // Settings

  private NativeKeyEvent activationKey;

  private String screenCoords;

  private HashMap<Character, ArrayList<TrainingImage>> trainingImages;

  private String frapsPath;

  private String outputFile;

  public GUIController(Settings settings) {
    this.settings = settings;

    // load settings
    this.activationKey = SettingsLoader.loadActivationKey(settings);
    this.screenCoords = SettingsLoader.loadScreenCoords(settings);
    this.trainingImages = SettingsLoader.loadTrainingImages(settings);
    this.frapsPath = SettingsLoader.loadFrapsScreenshotPath(settings);
    this.outputFile = SettingsLoader.loadOutputFilePath(settings);

    setLookandFeel();

    this.keyListener = new ActivationKeyListener(this);

    mainFrame = new MainFrame(this);
  }

  public void showGUI() {
    mainFrame.setVisible(true);
  }

  private void setLookandFeel() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception ex) {
      Logger.getLogger(GUIController.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public void repaint() {
    mainFrame.pack();
    mainFrame.repaint();
  }

  public void repaintKeepSize() {
    Dimension dim = mainFrame.getSize();
    mainFrame.pack();
    mainFrame.repaint();
    mainFrame.setSize(dim);
  }

  public Settings getSettings() {
    return settings;
  }

  public ActivationKeyListener getKeyListener() {
    return keyListener;
  }

  public NativeKeyEvent getActivationKey() {
    return activationKey;
  }

  public String getScreenCoords() {
    return screenCoords;
  }

  public HashMap<Character, ArrayList<TrainingImage>> getTrainingImages() {
    return trainingImages;
  }

  public void setActivationKey(NativeKeyEvent activationKey) {
    this.activationKey = activationKey;
  }

  public void setScreenCoords(String screenCoords) {
    this.screenCoords = screenCoords;
  }

  public void setTrainingImages(HashMap<Character, ArrayList<TrainingImage>> trainingImages) {
    this.trainingImages = trainingImages;
  }

  public String getFrapsPath() {
    return frapsPath;
  }

  public void setFrapsPath(String frapsPath) {
    this.frapsPath = frapsPath;
  }

  public MainPanel getMainPanel() {
    return mainFrame.getMainPanel();
  }

  public String getOutputFile() {
    return outputFile;
  }

  public void setOutputFile(String outputFile) {
    this.outputFile = outputFile;
  }
}
